package elements 
{
	import events.ElementEvent;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import gs.easing.Cubic;
	import gs.TweenLite;
	import styles.ButtonStyle;
	import styles.TextLinkStyle;
	import util.TextUtils;
	
	/**
	 * 
	 * @author Kasoi
	 */
	public class TextLink extends Sprite 
	{
		
		[Event(name = "_mouseOver", type = "events.ElementEvent")]
		[Event(name = "_mouseOut", type = "events.ElementEvent")]
		[Event(name = "_click", type = "events.ElementEvent")]
		[Event(name = "_press", type = "events.ElementEvent")]
		[Event(name = "_release", type = "events.ElementEvent")]
		
		////////////////////////////////////////////////////////////////////
		// Public properties
		////////////////////////////////////////////////////////////////////
		
		public function get field() : TextField 
		{
			return this._field;
		}
		
		public function get showPhase() : Number 
		{
			return this._showPhase;
		}
		
		public function set showPhase(value: Number) : void 
		{
			this._showPhase = value;
			
			var v: Number = 1 - value;
			var f: BlurFilter = new BlurFilter(5 * v, 5 * v, 2);
			
			this._field.filters = [f];
			this._field.alpha = value;
			
			if (value == 1) this._field.filters = [];
			
			//this._field.y = -50 * v;
		}
		
		public function get code() : String 
		{
			return this._code;
		}
		
		public function get style() : TextLinkStyle 
		{
			return this._style;
		}
		
		public function set style(value: TextLinkStyle) : void 
		{
			this._style = value;
			this.button_onMouseOut(null);
		}
		
		public function get itemHeight() : Number
		{
			return this._field.height;
		}
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var _field: TextField;
		
		private var container: Sprite;
		
		private var button: Button;
		
		private var _style: TextLinkStyle;
		
		private var _showPhase: Number = 0;
		
		private var _code: String;
		
		private var isHovered: Boolean = false;
		
		private var isPressed: Boolean = false;
		
		private var isSelected: Boolean = false;
		
		private var trigger: Sprite;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		public function TextLink(code: String, style: TextLinkStyle = null) 
		{
			if (style == null) style = new TextLinkStyle();
			this.style = style;
			this._code = code;
			this.trigger = new Sprite();
			
			var format: TextFormat = style.format;
			format.color = style.normalTextColor;
			
			this._field = TextUtils.makeOneLine(Texts.getText(code), format);
			this.container = new Sprite();
			this.container.addChild(this.field);
			
			this.field.mouseEnabled = false;
			
			this.trigger.buttonMode = true;
			
			this.trigger.addChild(this.container);
			
			this.addChild(this.trigger);
			
			this.showPhase = 0;
			
			this.trigger.addEventListener(MouseEvent.MOUSE_OVER, this.button_onMouseOver);
			this.trigger.addEventListener(MouseEvent.MOUSE_OUT, button_onMouseOut);
			this.trigger.addEventListener(MouseEvent.MOUSE_DOWN, this.button_onMouseDown);
		}
		
		public function select() : void 
		{
			this.mouseEnabled = this.mouseChildren = false;
			this.button_onMouseDown(null);
			this.isPressed = false;
			this.isSelected = true;
		}
		
		public function disselect() : void 
		{
			this.mouseEnabled = this.mouseChildren = true;
			this.isSelected = false;
			this.isPressed = false;
			this.button_onMouseOut(null);
		}
		
		public function show(delay: Number = 0) : void 
		{
			TweenLite.to(this, 1, { showPhase: 1, delay: delay, 
				ease: Cubic.easeOut } );
		}
		
		public function hide(delay: Number = 0) : void 
		{
			TweenLite.to(this, 1, { showPhase: 0, delay: delay, 
				ease: Cubic.easeOut } );
		}
		
		public function drawTrigger(x: Number, y: Number, width: Number, 
			height: Number) : void 
		{
			this.trigger.graphics.clear();
			this.trigger.graphics.beginFill(0xff00ff, 0.0);
			this.trigger.graphics.drawRect(x, y, width, height);
			this.trigger.graphics.endFill();
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function button_onMouseOver(e: MouseEvent) : void 
		{
			if (this.isPressed) return;
			if (this.isSelected) return;
			this.isHovered = true;
			TweenLite.to(this.container, this.style.hoverTime, 
				{ tint: this.style.hoverTextColor, ease: Cubic.easeOut } );
			this.dispatchEvent(new ElementEvent(ElementEvent.MOUSE_OVER));
		}
		
		private function button_onMouseOut(e: MouseEvent) : void 
		{
			if (this.isSelected) return;
			if (this.isPressed) return;
			TweenLite.to(this.container, this.style.hoverTime, 
				{ tint: this.style.normalTextColor, ease: Cubic.easeOut } );
			this.dispatchEvent(new ElementEvent(ElementEvent.MOUSE_OUT));
		}
		
		private function button_onMouseDown(e: MouseEvent) : void 
		{
			if (this.isSelected) return;
			TweenLite.to(this.container, this.style.activeTime, 
				{ tint: this.style.activeTextColor, ease: Cubic.easeOut } );
			this.isPressed = true;
			stage.addEventListener(MouseEvent.MOUSE_UP, this.button_onMouseUp);
			this.dispatchEvent(new ElementEvent(ElementEvent.PRESS));
		}
		
		private function button_onMouseUp(e: MouseEvent) : void 
		{
			if(stage) stage.removeEventListener(MouseEvent.MOUSE_UP, this.button_onMouseUp);
			if (this.isSelected) return;
			this.isPressed = false;
			
			var mx: Number = this.mouseX;
			var my: Number = this.mouseY;
			var b: Rectangle = this.getBounds(this);
			
			var inZone: Boolean = (mx > b.x && mx < b.x + b.width && my > b.y && my < b.y + b.height);
			
			if (!inZone)
			{
				this.isHovered = false;
				this.button_onMouseOut(null);
				this.dispatchEvent(new ElementEvent(ElementEvent.MOUSE_OUT));
			}
			else
			{
				TweenLite.to(this.container, this.style.activeTime, 
					{ tint: this.style.hoverTextColor, ease: Cubic.easeOut } );
				this.dispatchEvent(new ElementEvent(ElementEvent.MOUSE_OVER));
			}
		}
		
	}
	
}